/**
 * Author:yuiop
 * Date:2022/8/19 11:06
 */
package com.devops.admin.controller;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.alidns.model.v20150109.DescribeDomainsResponse;
import com.devops.admin.redis.RedisService;
import com.devops.common.utils.Message;
import com.devops.shiro.config.RedisManager;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.session.Session;
import org.crazycake.shiro.exception.SerializationException;
import org.crazycake.shiro.serializer.ObjectSerializer;
import org.crazycake.shiro.serializer.StringSerializer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;


@RestController
@RequestMapping("/redis")
@Api(value = "/redis",tags = "Redis相关接口")
public class RedisController {
    @Autowired
    RedisManager redisManager;
    @Autowired
    RedisService redisService;

    @GetMapping("/getSession/{sessionId}")
    @ApiOperation(value="获取登录用户的session",notes = "根据jsessionid从redis获取登录用户的session信息")
    @ApiImplicitParams({@ApiImplicitParam(name = "sessionId",value = "cookie中的JSESSIONID",required = true,dataType = "String",paramType = "path")})
    public Message getRedisByKey(@PathVariable("sessionId") String sessionId) throws SerializationException {
        Session s = (Session) new ObjectSerializer().deserialize(redisManager.get(new StringSerializer().serialize("shiro:session:"+sessionId)));
        return Message.ok(s);
    }

    @GetMapping("/save")
    public Message saveValueByRedis() {
        DescribeDomainsResponse.Domain domain1 = new DescribeDomainsResponse.Domain();
        domain1.setDomainId("1");
        domain1.setDomainName("967111");
        domain1.setRecordCount(100L);

        DescribeDomainsResponse.Domain domain2 = new DescribeDomainsResponse.Domain();
        domain2.setDomainId("2");
        domain2.setDomainName("qingyulan");
        domain2.setRecordCount(200L);

        ArrayList<DescribeDomainsResponse.Domain> list = new ArrayList<>();
        list.add(domain1);
        list.add(domain2);
        redisService.set("domain", JSONObject.toJSON(list).toString());
        return Message.ok(redisService.get("domain"));
    }
}
